/***********************************************************
 
 File name: UIAlertController+Extend.m
 Author:    xuwei
 Description:
 alertView的扩展
 
 2018/02/28: Created
 
 ************************************************************/

#import "UIAlertController+Extend.h"

@implementation UIAlertController (Extend)
/**
 设置标题颜色和字体
 
 @param color 颜色
 @param font 字体
 */
- (void)setTitleColor:(UIColor *)color font:(UIFont *)font {
    if (self.title) {
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.title];
        [string addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, self.title.length)];
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.title.length)];
        
        if ([self isVersionOK]) {
            NSString *key = @"attributedTitle";
            [self setValue:string forKey:key];
        }
    }
}

/**
 设置正文的颜色和字体
 
 @param color 颜色
 @param font 字体
 */
- (void)setMessageColor:(UIColor *)color font:(UIFont *)font {
    if (self.message) {
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.message];
        [string addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, self.message.length)];
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.message.length)];
        
        if ([self isVersionOK]) {
            NSString *key = @"attributedMessage";
            [self setValue:string forKey:key];
        }
    }
}

/**
 设置Action的颜色
 
 @param action action对象
 @param color 颜色
 */
- (void)addAction:(UIAlertAction *)action color:(UIColor *)color {
    if ([self isVersionOK]) {
        NSString *key = @"_titleTextColor";
        [action setValue: color forKey:key];
    }
    [self addAction:action];
}

/**
 判断版本是否正确

 @return YES：8.3以上版本  NO：8.3以下版本
 */
- (BOOL)isVersionOK {
    double version = [[[UIDevice currentDevice] systemVersion] doubleValue];
    if (version >= 8.2999999) {
        return YES;
    }
    return NO;
}
@end
